package com.yonyou.cyxdms.sysAuth.service.service.dealerchannel;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yonyou.cyx.framework.util.bean.ApplicationContextHelper;
import com.yonyou.cyx.function.exception.DALException;
import com.yonyou.cyx.function.exception.ServiceBizException;
import com.yonyou.cyx.function.utils.bean.BeanMapperUtil;
import com.yonyou.cyx.function.utils.common.CommonUtils;
import com.yonyou.cyx.function.utils.common.StringUtils;
import com.yonyou.cyxdms.framework.bean.dto.user_info.LoginInfoDto;
import com.yonyou.cyxdms.sysAuth.service.constants.CommonConstants;
import com.yonyou.cyxdms.sysAuth.service.constants.ManageDictCodeConstants;
import com.yonyou.cyxdms.sysAuth.service.dto.CompanyDTO;
import com.yonyou.cyxdms.sysAuth.service.dto.dealerchannel.TmDealerchannelDTO;
import com.yonyou.cyxdms.sysAuth.service.entity.*;
import com.yonyou.cyxdms.sysAuth.service.mapper.*;
import com.yonyou.cyxdms.sysAuth.service.service.SystemParamService;
import com.yonyou.cyxdms.sysAuth.service.service.manage.employee.EmployeeService;
import com.yonyou.cyxdms.sysAuth.service.service.role.RoleService;
import com.yonyou.cyxdms.sysAuth.service.vo.dealerchannel.DealerChannelDetailVO;
import com.yonyou.cyxdms.sysAuth.service.vo.employee.AddRolePO;
import com.yonyou.cyxdms.sysmanage.client.constants.BaseDataDictConstants;
import com.yonyou.cyxdms.sysmanage.client.domains.dto.SmsPushDTO;
import com.yonyou.cyxdms.sysmanage.client.service.login.PushServices;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional(rollbackFor = Exception.class)
public class DealerChannelServiceImpl implements DealerChannelService {
    private final Logger logger = LoggerFactory.getLogger(DealerChannelServiceImpl.class);
    @Autowired
    DealerchannelMapper dealerchannelMapper;
    @Autowired
    CompanyMapper companyMapper;
    @Autowired
    EmpMapper empMapper;
    @Autowired
    EmployeeMapper employeeMapper;
    @Autowired
    RoleMapper roleMapper;
    @Autowired
    private OrgMapper orgMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private TrOrgMapper trOrgMapper;
    @Autowired
    private UserOrgMapper userOrgMapper;
    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private PushServices pushService;
    @Autowired
    DealerInfoMapper dealerInfoMapper;
    @Autowired
    RoleService roleService;
    @Autowired
    SystemParamService systemParamServices;


    /**
     * 查询-经销商公司维护列表(分页)
     *
     * @author adu
     * @since 2019/10/12
     */
    @Override
    public IPage<CompanyPO> getDistributorList(Page page, Map<String, Object> queryCondition) {
        // 获取总条数
        page.setRecords(companyMapper.getDistributorList(page, queryCondition));
        return page;
    }

    @Override
    public CompanyDTO getCompanyId(Integer companyId) {
        LoginInfoDto loginInfoDto = ApplicationContextHelper.getBeanByType(LoginInfoDto.class);
        return companyMapper.getCompanyId(companyId, loginInfoDto.getAppId(), loginInfoDto.getDataType());
    }

    /**
     * 新增-经销商公司维护
     *
     * @author adu
     * @since 2019/10/12
     */
    @Override
    public Integer addCompany(CompanyDTO companyDTO) {
//        Integer companyId = companyDTO.getCompanyId();
        Integer companyId = 0;
        if (companyId == null) {

            // 0、插入company表信息
//            Integer comId = setCompanyInfo(companyDTO);
            Integer comId = 0;

            //0-1 插入tm_org表信息
            Long orgId = setOrgInfos(comId, companyDTO);

            this.updateCompanyOrgId(orgId, comId);

            this.setTrOrgInfo(orgId);

            // 1、插入emp表信息（插入手机号及info）
            Long empId = setEmpInfo(comId, companyDTO);

            // 2、插入tm_employee表信息
            Long employeeId = setEmployeeInfo(comId, empId, orgId, companyDTO);

            // 3、插入USER表信息(插入账号及密码信息)
            Integer userId = setUserInfo(empId, comId, companyDTO);

            // 4、插入user_org表信息
            Long userOrgId = setUserOrgInfos(userId, orgId, companyDTO.getCompanyCode());

            // 5、插入tr_org_role表信息
            this.setOrgRoleInfo(orgId, employeeId, userOrgId);

            //6、插入sysparam
            systemParamServices.insertInitParam(comId.toString());

            // 发送登录账户、密码信息
            String mobilePhone = "";
            if (!StringUtils.isNullOrEmpty(mobilePhone)) {
                sendSmsCount(companyDTO);
            }
            return comId;
        } else {
            // 6、更新公司表信息
//            return updateCompanyInfo(companyId, companyDTO);
            return 0;
        }
    }

    // 发送登录账户、密码信息
    public void sendSmsCount(CompanyDTO companyDTO) {
        SmsPushDTO smsPushDTO = new SmsPushDTO();
        smsPushDTO.setMobiles("");
        smsPushDTO.setTemplateId(CommonConstants.SMS_CODE);
        Map<String, String> params = new HashMap<>();
        params.put("1", companyDTO.getUserCode());
        params.put("2", "");
        params.put("3", CommonConstants.SMS_URL);
        smsPushDTO.setParamMap(params);
        pushService.sendSms(smsPushDTO);
        logger.info("公告推送成功：{}" + smsPushDTO.toString());
    }

    /**
     * 0、插入company表信息（插入手机号及info）
     */
    public Integer setCompanyInfo(CompanyDTO companyDTO) {
        LoginInfoDto loginInfoDto = ApplicationContextHelper.getBeanByType(LoginInfoDto.class);
        // Long factoryCompanyId = companyMapper.findFactoryIdByAppId(loginInfoDto.getAppId());
        //插入公司信息

        CompanyPO companypo = new CompanyPO();
        BeanMapperUtil.copyProperties(companyDTO, companypo);
        companypo.setIsDeleted(0);
        companypo.setFactoryCompanyId(1);
        companypo.setAppId(loginInfoDto.getAppId());
//        companyPO.setOrgId(Integer.MAX_VALUE);
        companypo.setValidStatus(companyDTO.getValidStatus());
        companypo.setCompanyType(ManageDictCodeConstants.ORG_TYPE_DEALER_COMPANY);
        companypo.setDataType(ManageDictCodeConstants.DATA_TYPE_BY_OWNER);
        companypo.setIsFixed(10041002);
        companypo.setDealerType(25381001);
//        companyPO.setAddressZh(companyDTO.getAddressZh());
        companypo.setCreatedBy(loginInfoDto.getUserName());
        companypo.setUpdatedBy(loginInfoDto.getUserName());
        companyMapper.insert(companypo);
        logger.debug("新增经销商公司信息：{}", companypo);
//        Long factoryCompanyId = companyMapper.findFactoryIdByAppId(loginInfoDto.getAppId());
//        DealerInfoPO dealerInfoPO = new DealerInfoPO();
//        dealerInfoPO.setCompanyId(companyPO.getCompanyId());
//        dealerInfoPO.setCityId(companyDTO.getCityId());
//        dealerInfoPO.setAddress(companyDTO.getAddressCn());
//        dealerInfoPO.setEmail(companyDTO.geteMail());
//        dealerInfoPO.setFaxNo(companyDTO.getFax());
//        dealerInfoPO.setPhone(companyDTO.getPhone());
//        dealerInfoPO.setOrgType(ManageDictCodeConstants.ORG_TYPE_DEALER);
//        dealerInfoPO.setFactoryCompanyId(factoryCompanyId.intValue());
//        dealerInfoPO.setOrgId(companyPO.getOrgId());
//        dealerInfoPO.setDealerCode(companyDTO.getCompanyCode());
//        dealerInfoPO.setDealerName(companyDTO.getCompanyNameCn());
//        dealerInfoPO.setDealerShortname(companyDTO.getCompanyShortNameCn());
//        dealerInfoPO.setAppId(loginInfoDto.getAppId());
//        dealerInfoPO.setValidStatus(CommonConstants.DEALER_ATTACH_STATUS_ENABLE);
//        dealerInfoPO.setStatus(CommonConstants.DEALER_ATTACH_STATUS_ENABLE);
//        dealerInfoPO.setCreatedAt(LocalDateTime.now());
//        dealerInfoPO.setIsDeleted(0);
//        dealerInfoService.addDealerInfo(dealerInfoPO);
        return companypo.getCompanyId();
    }

    //0-1插入tm_org表信息
    public Long setOrgInfos(Integer comId, CompanyDTO companyDTO) {
        LoginInfoDto loginInfoDto = ApplicationContextHelper.getBeanByType(LoginInfoDto.class);
        //新增tmOrg
        OrgPO orgpo = new OrgPO();
        orgpo.setAppId(loginInfoDto.getAppId());
        orgpo.setCompanyId(comId.longValue());
        orgpo.setOrgCode(companyDTO.getCompanyCode());
        orgpo.setOrgName(companyDTO.getCompanyNameCn());
        orgpo.setOrgShortName(companyDTO.getCompanyShortNameCn());
        orgpo.setOrgType(ManageDictCodeConstants.ORG_TYPE_DEALER_COMPANY);
        orgpo.setIsValid(ManageDictCodeConstants.STATUS_IS_VALID);
        orgpo.setDataType(ManageDictCodeConstants.DATA_TYPE_BY_OWNER);
        orgpo.setIsDeleted(0);
        orgpo.setOrgId(Long.MAX_VALUE);
        orgMapper.insert(orgpo);
        return orgpo.getOrgId();
    }

    public void setTrOrgInfo(Long orgId) {
        TrOrgPO trorgpo = new TrOrgPO();
        trorgpo.setOrgId(orgId);
        trorgpo.setParentOrgId(0L);
        trOrgMapper.insert(trorgpo);
    }


    /**
     * 1、插入emp表信息（插入手机号及info）
     */
    public Long setEmpInfo(Integer companyId, CompanyDTO companyDTO) {
        LoginInfoDto loginInfoDto = ApplicationContextHelper.getBeanByType(LoginInfoDto.class);
        EmpPo empPo = new EmpPo();
        empPo.setAppId(loginInfoDto.getAppId());
        empPo.setCompanyId(companyId);
        empPo.setEmployeeName(companyDTO.getCompanyCode());
//        empPo.setMobilePhone(companyDTO.getMobilePhone());
        empPo.setIsOnjob(ManageDictCodeConstants.EMPLOYEE_ISJOB);
        empPo.setUserStatus(ManageDictCodeConstants.STATUS_QY);
        empPo.setDataSources(ManageDictCodeConstants.DATA_SOURCES_BY_OWNER);
        empMapper.insert(empPo);
        return empPo.getEmpId();
    }

    /**
     * 2、插入employee表信息
     */
    public Long setEmployeeInfo(Integer companyId, Long empId, Long orgId, CompanyDTO companyDTO) {
        LoginInfoDto loginInfoDto = ApplicationContextHelper.getBeanByType(LoginInfoDto.class);
        // Integer orgId = employeeMapper.getOrgId(loginInfoDto.getAppId(), companyDTO.getCompanyCode(), companyId);
        EmployeePO employeepo = new EmployeePO();
        employeepo.setAppId(loginInfoDto.getAppId());
        employeepo.setCompanyId(Long.valueOf(companyId));
        employeepo.setEmpId(empId);
        employeepo.setOrgId(orgId);
        employeepo.setEmployeeNo(companyDTO.getCompanyCode());
        employeepo.setIsMajor(BaseDataDictConstants.IS_VALID);
        employeepo.setValidStatus(BaseDataDictConstants.IS_VALID);
        employeepo.setEmployeeStatus(10081001);
        employeepo.setDataType(ManageDictCodeConstants.DATA_TYPE_BY_OWNER);
        employeepo.setOwnerCode(companyDTO.getCompanyCode());
        employeeMapper.insert(employeepo);
        return employeepo.getEmployeeId();
    }


    /**
     * 3、插入USER表信息(插入账号及密码信息)
     */
    public Integer setUserInfo(Long empId, Integer companyId, CompanyDTO companyDTO) {
        LoginInfoDto loginInfoDto = ApplicationContextHelper.getBeanByType(LoginInfoDto.class);
        UserPO userpo = new UserPO();
        userpo.setAppId(loginInfoDto.getAppId());
        userpo.setEmpId(empId.intValue());
        userpo.setCompanyId(companyId.longValue());
        userpo.setUserCode(companyDTO.getUserCode());
//        userPO.setPassword(MD5Util.getEncryptedPwd(companyDTO.getPassword()));
        userpo.setUserStatus(ManageDictCodeConstants.STATUS_QY);
        userpo.setFailureNum(0);
        userpo.setLockFlag(10041002);
        userpo.setIsDeleted(0);
        userMapper.insert(userpo);
        return userpo.getUserId();
    }

    /**
     * 4、插入user_org表信息
     */
    public Long setUserOrgInfos(Integer userId, Long orgId, String companyCode) {
        UserOrgPO userorgpo = new UserOrgPO();
        userorgpo.setUserId(userId.longValue());
        userorgpo.setOrgId(orgId);
        userorgpo.setOwnerCode(companyCode);
        userorgpo.setOwnerOrgType(ManageDictCodeConstants.ORG_TYPE_DEALER_COMPANY);
        userorgpo.setIsDeleted(0);
        userOrgMapper.insert(userorgpo);
        return userorgpo.getUserOrgId();
    }

    /**
     * 5、插入tr_org_role表信息
     */
    public Long setOrgRoleInfo(Long orgId, Long employeeId, Long userOrgId) {
        EmployeePO employeepo = employeeMapper.selectById(employeeId);
        Integer roleId = addAdminRole();//初始化角色001
        List<Long> roleIds = new ArrayList<>();
        roleIds.add(roleId.longValue());
        AddRolePO addrolepo = new AddRolePO();
        addrolepo.setEmployeeNo(employeepo.getEmployeeNo());
        addrolepo.setOrgId(orgId);
        addrolepo.setUserOrgId(userOrgId);
        addrolepo.setRoleId(roleIds);
        employeeService.addRoles(addrolepo);
        return orgId;
    }

    /**
     * 新增经销商公司维护-初始化角色GLY001
     *
     * @return
     */
    public Integer addAdminRole() {
        LoginInfoDto loginInfo = ApplicationContextHelper.getBeanByType(LoginInfoDto.class);
        String roleCode = "GLY001";
        Integer roleId = roleMapper.selectByRoleId(roleCode, loginInfo.getAppId());
        if (roleId == null) {
            throw new DALException("没有找到默认的角色！");
        }
        return roleId;
    }


    /**
     * 6、更新公司表信息
     */
    public Integer updateCompanyInfo(Integer companyId, CompanyDTO companyDTO) {
        LoginInfoDto loginInfoDto = ApplicationContextHelper.getBeanByType(LoginInfoDto.class);
        CompanyPO companypo1 = new CompanyPO();
        Long factoryCompanyId = companyMapper.findFactoryIdByAppId(loginInfoDto.getAppId());
        if (null == factoryCompanyId) {
            throw new ServiceBizException("您输入的AppID可能不存在");
        }
        //对对象进行赋值操作
        BeanUtils.copyProperties(companyDTO, companypo1);
        logger.debug("获取dealerInfoDTO的信息dealerInfoPO:{}", companypo1);
        //根据的appid 和 dealercode获取company明细
        LambdaQueryWrapper<CompanyPO> queryWrapper = new QueryWrapper<CompanyPO>().lambda();
        queryWrapper.eq(CompanyPO::getAppId, loginInfoDto.getAppId());
        queryWrapper.eq(CompanyPO::getCompanyCode, companyDTO.getCompanyCode());
        CompanyPO companypo = companyMapper.selectOne(queryWrapper);
        companypo.setAddressCn(companyDTO.getAddressCn());
        companypo.setProvinceId(companyDTO.getProvinceId());
        companypo.setCityId(companyDTO.getCityId());
//        companyPO.setCounty(companyDTO.getCounty());
        companypo.setCompanyCode(companyDTO.getCompanyCode());
        companypo.setCompanyNameCn(companyDTO.getCompanyNameCn());
        companypo.setCompanyShortNameCn(companyDTO.getCompanyShortNameCn());
        companypo.setCompanyType(ManageDictCodeConstants.ORG_TYPE_DEALER_COMPANY);
        companypo.setDataType(ManageDictCodeConstants.DATA_TYPE_BY_OWNER);
        companypo.seteMail(companyDTO.geteMail());
//        companyPO.setAddressZh(companyDTO.getAddressZh());
        companypo.setValidStatus(companyDTO.getValidStatus());
        companypo.setCompanyHomepage(companyDTO.getCompanyHomepage());
        companypo.setBusinessHours(companyDTO.getBusinessHours());
//        companyPO.setOpenDate(companyDTO.getOpenDate());
        companypo.setRemark(companyDTO.getRemark());
        companypo.setZipCode(companyDTO.getZipCode());
        companypo.setFax(companyDTO.getFax());
        companypo.setPhone(companyDTO.getPhone());
        companypo.setUpdatedAt(LocalDateTime.now());
        companyMapper.updateById(companypo);
        //没有符合条件条数据---新增操作---返回主键id
        return companypo.getCompanyId();
    }

    public void updateCompanyOrgId(Long orgId, Integer comId) {
        LambdaQueryWrapper<CompanyPO> queryWrapper = new QueryWrapper<CompanyPO>().lambda();
        queryWrapper.eq(CompanyPO::getCompanyId, comId);
        CompanyPO companypo = companyMapper.selectOne(queryWrapper);
//        companyPO.setOrgId(orgId.intValue());
        companyMapper.updateById(companypo);
    }

    public void setpo(DealerchannelPO dealerchannelpo, TmDealerchannelDTO dealerDto) {

        dealerchannelpo.setCompanyId(dealerDto.getCompanyId());
        dealerchannelpo.setDealerCompanyId(dealerDto.getDealerCompanyId());
        if (dealerDto.getSalesAuthority() != null) {
            dealerchannelpo.setSalesAuthority(Integer.parseInt(dealerDto.getSalesAuthority()));
        }
        if (dealerDto.getAfterSaleAuthority() != null) {
            dealerchannelpo.setAfterSaleAuthority(Integer.parseInt(dealerDto.getAfterSaleAuthority()));
        }

        dealerchannelpo.setCustomerType(dealerDto.getCustomerType());
        if (dealerDto.getSalesOrganization() != null) {
            dealerchannelpo.setSalesOrganization(Integer.parseInt(dealerDto.getSalesOrganization()));
        }

        dealerchannelpo.setSalesName(dealerDto.getSalesName());

        if (!StringUtils.isNullOrEmpty(dealerDto.getSalesOrganization())) {
            dealerchannelpo.setSalesName(getCodeName(dealerDto.getSalesOrganization()));
        }
        if (dealerDto.getParentDealerId() != null) {
            dealerchannelpo.setParentDealerId(dealerDto.getParentDealerId().intValue());
        }
        dealerchannelpo.setSalesArea(dealerDto.getSalesArea());
        dealerchannelpo.setAfterSaleArea(dealerDto.getAfterSaleArea());
        dealerchannelpo.setSalesCode(dealerDto.getSalesCode());
        dealerchannelpo.setAfterSaleCode(dealerDto.getAfterSaleCode());
        dealerchannelpo.setDealerName(dealerDto.getDealerName());
        dealerchannelpo.setAddress(dealerDto.getAddress());
        dealerchannelpo.setShortName(dealerDto.getShortName());
        dealerchannelpo.setProvince(dealerDto.getProvince());
        dealerchannelpo.setCity(dealerDto.getCity());
        dealerchannelpo.setCounty(dealerDto.getCounty());
        dealerchannelpo.setBzirk(dealerDto.getBzirk());
        dealerchannelpo.setKdgrp(dealerDto.getKdgrp());
        dealerchannelpo.setTaxnumxl(dealerDto.getTaxnumxl());
        dealerchannelpo.setHomeCity(dealerDto.getHomeCity());
        dealerchannelpo.setHouseNum2(dealerDto.getHouseNum2());
        dealerchannelpo.setMobileNumber(dealerDto.getMobileNumber());
        dealerchannelpo.setAccountBankName(dealerDto.getAccountBankName());
        dealerchannelpo.setBankAccount(dealerDto.getBankAccount());
        dealerchannelpo.setStreet(dealerDto.getStreet());
        dealerchannelpo.setTelNumber(dealerDto.getTelNumber());
        dealerchannelpo.setSalesAccountBankName(dealerDto.getSalesAccountBankName());
        dealerchannelpo.setSalesAccountBank(dealerDto.getSalesAccountBank());
        dealerchannelpo.setAfterSalesAccountBankName(dealerDto.getAfterSalesAccountBankName());
        dealerchannelpo.setAfterSalesAccountBank(dealerDto.getAfterSalesAccountBank());
        dealerchannelpo.setBuilding(dealerDto.getBuilding());
        dealerchannelpo.setLocation(dealerDto.getLocation());
        dealerchannelpo.setCity2(dealerDto.getCity2());
        dealerchannelpo.setStrSuppl2(dealerDto.getStrSuppl2());
        dealerchannelpo.setStrSuppl3(dealerDto.getStrSuppl3());
        dealerchannelpo.setRoomnumber(dealerDto.getRoomnumber());
        dealerchannelpo.setNameCo(dealerDto.getNameCo());
        dealerchannelpo.setFloor(dealerDto.getFloor());
        dealerchannelpo.setStrSuppl1(dealerDto.getStrSuppl1());
        dealerchannelpo.setHouseNum1(dealerDto.getHouseNum1());
        dealerchannelpo.setBahns(dealerDto.getBahns());
        dealerchannelpo.setKatr1(dealerDto.getKatr1());
        dealerchannelpo.setKatr2(dealerDto.getKatr2());
        dealerchannelpo.setKatr3(dealerDto.getKatr3());
        dealerchannelpo.setLocco(dealerDto.getLocco());
        dealerchannelpo.setSmtpAddr(dealerDto.getSmtpAddr());
        dealerchannelpo.setFreeze(dealerDto.getFreeze());
        dealerchannelpo.setDesignatedAfterSalesDealer(dealerDto.getDesignatedAfterSalesDealer());
        dealerchannelpo.setDealerStarLevel(dealerDto.getDealerStarLevel());
    }

    private String getCodeName(String id) {
        String codeName = "";
        List<Map<String, Object>> list = dealerchannelMapper.getCodeName(id);
        if (!CommonUtils.isNullOrEmpty(list)) {
            codeName = (String) list.get(0).get("CODE_CN_DESC");
        }
        return codeName;
    }

    /**
     * 修改经销商渠道信息
     *
     * @author dengqiang
     * @since 2019/7/23
     */
    @Override
    public Integer editDealerChannel(Long id, TmDealerchannelDTO dealerDto) {
        DealerchannelPO dealerchannelpo = dealerchannelMapper.selectById(id);
        setpo(dealerchannelpo, dealerDto);
        dealerchannelMapper.updateById(dealerchannelpo);
        return dealerchannelpo.getDealerId().intValue();
    }

    /**
     * 按照ID查询经销商信息
     *
     * @author dengqiang
     * @since 2019/7/24
     */
    @Override
    public DealerChannelDetailVO getDealerChannelById(Long id) {
        return dealerchannelMapper.getDealerChannelById(id);
    }

    @Override
    public List<Map> queryDealerInfoExport(Map<String, String> queryParam) {

        return dealerchannelMapper.queryDealerInfoExport(queryParam);
    }

    /**
     * 查询所属经销商
     *
     * @return
     */
    @Override
    public List<Map<String, Object>> getDistributor() {
        LoginInfoDto loginInfoDto = ApplicationContextHelper.getBeanByType(LoginInfoDto.class);
        return companyMapper.getDistributor(loginInfoDto.getAppId(), ManageDictCodeConstants.ORG_TYPE_DEALER_COMPANY);
    }

    /**
     * 查询所属经销商ALL
     *
     * @return
     */
    @Override
    public List<Map<String, Object>> getdistributorall() {
        //LoginInfoDto loginInfoDto = ApplicationContextHelper.getBeanByType(LoginInfoDto.class);
        return companyMapper.getDistributorALL();
    }

    /**
     * 查询所属经销商(售后)
     *
     * @return
     */
    @Override
    public List<Map<String, Object>> getDistributors() {
        LoginInfoDto loginInfoDto = ApplicationContextHelper.getBeanByType(LoginInfoDto.class);
        return companyMapper.getDistributors(loginInfoDto.getAppId(), ManageDictCodeConstants.ORG_TYPE_DEALER_COMPANY);
    }

    /**
     * 查询-下发经销商
     * 2019-10-29 adu
     */
    @Override
    public IPage<Map<String, Object>> getDealerInfos(Page page, Map<String, Object> queryCondition) {
        // 获取总条数
        page.setRecords(companyMapper.getDealerInfos(page, queryCondition));
        return page;
    }

    @Override
    public Integer findDealerId(String ownerCode) {
        LambdaQueryWrapper<DealerInfoPO> queryWrapper = new QueryWrapper<DealerInfoPO>().lambda();
        queryWrapper.select(DealerInfoPO::getDealerId).eq(DealerInfoPO::getDealerCode, ownerCode);
        DealerInfoPO dealerinfopo = dealerInfoMapper.selectOne(queryWrapper);
        if (dealerinfopo == null) {
            return -1;
        }
        return dealerinfopo.getDealerId();
    }

    @Override
    public String getGroupCodeByAppId(String appId)
    {
        return companyMapper.getGroupCodeByAppId(appId);
    }
}
